package cn.marinda.spritefragment.data;

import cn.lovelycatv.custom.CustomItemObject;
import cn.lovelycatv.custom.SoltObject;
import cn.marinda.spritefragment.SpriteFragment;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.Plugin;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Array;
import java.security.Key;
import java.util.*;

public class ExchangeConfig implements DataObject,DataInterface{
    /*
    合成公式管理
     */
    private File file = null;
    private YamlConfiguration exchangeConfig = null;
    private Set<String> keys = null;
    private Map<String,Integer> number = null;
    private Map<String,String> mark = null;
    private ConfigurationSection cs = null;
    private Set<String> slotkey = null;
    private List<String> slotList = null;
    private Map<String,List<String>> slotMap = null;
    private Map<String,Integer> payNumber = null;
    public ExchangeConfig(){
        defaultConfig();
        keys = new HashSet<>();
        slotkey = new HashSet<>();
        mark = new HashMap<>();
        number = new HashMap<>();
        slotList = new ArrayList<>();
        slotMap = new HashMap<>();
        payNumber = new HashMap<>();
        defaultGetConfig();
    }

    private Map<String, CustomItemObject> map = new HashMap<>();

    //获取config
    @Override
    public YamlConfiguration getConfig() {
        return exchangeConfig;
    }

    @Override
    public void defaultConfig() {
        exchangeConfig = new YamlConfiguration();
        try {
            exchangeConfig.load(new File(SpriteFragment.plugin.getDataFolder(),"exchange.yml"));
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    public Map<String, CustomItemObject> getMap() {
        return map;
    }

    @Override
    public void defaultGetConfig() {
        //最外层父级键
        keys = getConfig().getKeys(false);
        //遍历最外层父级键 这里keyName则是TestN
        for(String keyName : keys){
            //configutionSection选择到了这个TestN键
            cs = getConfig().getConfigurationSection(keyName);
            //取到TestN下的.info
            ConfigurationSection ss = getConfig().getConfigurationSection(cs.getName() + ".info");
            //slotKey = 取到TestN.info下的所有slot 则就是slot1-5
            slotkey = getConfig().getConfigurationSection(ss.getCurrentPath()).getKeys(false);
            ConfigurationSection selectPayNumber = getConfig().getConfigurationSection(cs.getName() + ".condition");;
            payPut(keyName,getConfig().getInt(selectPayNumber.getCurrentPath() + ".pay"));
            int i = 1;
            CustomItemObject customItemObject = new CustomItemObject();
            Map<Integer,SoltObject> solts = new HashMap<>();
            for (String soltName : slotkey) {
                String mark = getConfig().getString(keyName + ".info." + soltName + ".mark");
                int num = getConfig().getInt(keyName + ".info." + soltName + ".number");
                solts.put(i,new SoltObject(mark,num));
                i++;
            }
            customItemObject.setSolts(solts);
            map.put(keyName,customItemObject);
        }
    }

    public Map<String, Integer> getNumber() {
        return number;
    }

    public Map<String, String> getMark() {
        return mark;
    }

    //number
    public void putNumber(String key, int number){
        if(!getNumber().containsKey(key)){
            getNumber().put(key,number);
        }
    }
    //Mark
    public void putMark(String key,String mark){
        if(!getMark().containsKey(key)){
            getMark().put(key,mark);
        }
    }
    //put方法
    public void slotListAdd(String slotName){
        if(!slotList.contains(slotName)){
            slotList.add(slotName);
        }
    }
    //put方法
    public void slotPut(String keyName,List<String> slotLists){
        if(!slotMap.containsKey(keyName)){
            slotMap.put(keyName,slotLists);
        }
    }
    //put方法
    public void payPut(String keyName,int payNumber){
        if(!this.payNumber.containsKey(keyName)){
            this.payNumber.put(keyName,payNumber);
        }

    }

    public Map<String, Integer> getPayNumber() {
        return payNumber;
    }

    @Override
    public Set<String> getKeys() {
        return keys;
    }

    @Override
    public ItemStack getResultItems(String key) {
        return null;
    }

    @Override
    public ItemStack getItems(String key) {
        return null;
    }

    @Override
    public int getItemPayNumber(String key) {
        if(payNumber.containsKey(key)){
            return  getPayNumber().get(key);
        }
        return -1;
    }

    @Override
    public String getKeyMark(int index,String parrentKey) {
        if(map.get(parrentKey).getSolts().containsKey(index)){
            if(new ConfigManager().getItemData().getKeys().contains(map.get(parrentKey).getSolts().get(index).getMark())){
                return map.get(parrentKey).getSolts().get(index).getMark();
            }
        }
        return null;
    }

    @Override
    public int getNumber(int index, String parrentKey) {
        if(map.get(parrentKey).getSolts().containsKey(index)){
            if(new ConfigManager().getItemData().getKeys().contains(map.get(parrentKey).getSolts().get(index).getMark())){
                return map.get(parrentKey).getSolts().get(index).getNum();
            }
        }
        return -1;
    }

    @Override
    public String getExchangeResule(String key) {
        return null;
    }
}
